<?php
namespace Todo\Controller;
use Todo\Builder\FormBuilder;
use Todo\Builder\ListBuilder;
use Todo\PHPExcel\Helper;
use Todo\Service\ExportService;
use Todo\Service\LoginService;
use Todo\Service\LogService;
use Think\Page;

/**
 * Class StatsController
 * 统计查询应用基类控制器
 * @package Todo\Controller
 */
class StatsController extends BaseController {

    protected $_user = null;
    protected $_model = null;

    /**
     * 导出数据的格式化要求
     * @var string
     */
    protected $_format = '';

    /**
     * 导出数据的列表头
     * @var array
     * array(
     * array(字段名，标题名称)
     * )
     */
    protected $_header = array();
    protected $_limit = 1000;


    protected function _initialize(){
        parent::_initialize();

        $this->_user = LoginService::getInstance()->getLoginUser(true);
        define('UID', $this->_user['id']);

        $appInfo = D('Todo/Sys/Application')->keyFind(C('APP_ID'));
        $this->assign('_app', $appInfo);
    }

    /**
     * 报表
     */
    public function index(){
        $this->display();
    }

    /**
     * 查询
     */
    public function query(){
        $this->display();
    }

    /**
     * 导出
     */
    public function export(){
        $this->_loadModel();
        $where = $this->_search();
        $this->_filter($where);
        $this->_pager();
        $list = $this->_model->pageSelect($where, $this->_page, $this->_limit, $this->_order);
        foreach ($list as &$row){
            $this->_model->format($row, $this->_format);
        }
        $helper = new Helper();
        $helper->exportExcel('用户列表', $this->_header, $list);
    }

    /**
     * 根据表单生成查询条件
     * 进行列表过滤
     * @access protected
     * @param  string $name 数据对象名称
     * @return array
     */
    protected function _search($name = '')
    {
        $this->_loadModel();
        $map = array();//$map = array ('terminalId' => TNT_ID);
        foreach ($this->_model->getDbFields() as $key => $val) {
            if (isset ($_REQUEST [$val]) && $_REQUEST [$val] != '') {
                $map [$val] = $_REQUEST [$val];
            }
        }
        if ($map['id'] && is_string($map['id'])) $map['id'] = array('in', explode(',', $map['id']));
        unset($map['sort']);
        return $map;
    }

    /**
     * 自定义查询条件
     * @param array &$where 丢生成的查询条件
     */
    protected function _filter(&$where){}

    /**
     * 设置grid的分页信息
     */
    protected function _pager()
    {
        if (isset($_REQUEST['page']) && !empty($_REQUEST['page'])) {
            $this->_page = intval($_REQUEST['page']);
        }

        if (isset($_REQUEST['limit']) && !empty($_REQUEST['limit'])) {
            $this->_limit = intval($_REQUEST['limit']);
        }

        if (isset($_REQUEST['sort']) && !empty($_REQUEST['sort'])) {
            $this->_order = $_REQUEST['sort'];
            if (isset($_REQUEST['order']) && !empty($_REQUEST['order'])) {
                $this->_order .= ' ' . $_REQUEST['order'];
            }
        }
    }

    /**
     * 加载模型类，需要重写
     */
    protected function _loadModel(){}
}